package com.intel.factorytest.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.intel.factorytest.R;

public class AmbientLightSensor extends BaseActivity{

    
    private SensorManager mSensorManager;
    private Sensor aLSSensor;
    
    private TextView testText;
    
    private static final String TAG = "ShowALS";
    
    private SensorEventListener aLSListener = new SensorEventListener() {

//        @Override
        public void onSensorChanged(SensorEvent event) {
//            float value_0 = event.values[0];
            int value_0 = (int)event.values[0];
//            long timestamp = event.timestamp;
//            Log.i(TAG, "onSensorChanged, value_0: " + value_0);
            Log.i(TAG, "onSensorChanged, timestamp: " + value_0);
            
            testText.setText(getString(R.string.SERVICEMENU_ALS_VALUE_TXT) + " " +value_0);
            
        
        } //end onSensorChanged
        
//        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub
            
        }        
    }; //end aLSListener
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        aLSSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

        testText = new TextView(this);
        testText.setTextSize(24.0f);
         FrameLayout.LayoutParams l = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
         l.gravity = Gravity.CENTER;
        testText.setLayoutParams(l);
        
        if (aLSSensor == null) {
            Log.d(TAG, "Cannot detect sensors. Not enabled");
            testText.setText(R.string.SERVICEMENU_ALSENSOR_NULL_TXT);            
//            Create listener only if sensors do exist            
        }else
        {
            Log.i(TAG, "ALS sensors avaliable"); 
            testText.setText(getString(R.string.SERVICEMENU_ALS_START_TXT));
        }
        FrameLayout frame = (FrameLayout)this.getWindow().getDecorView().findViewById(android.R.id.content);
        frame.addView(testText);
//        setContentView(testText);            

    } // end onCreate
    
    @Override
    public void onResume()
    {
//        if (Config.LOGD) Log.d(TAG, "onResume");
        super.onResume();
        mSensorManager.registerListener(aLSListener, aLSSensor, SensorManager.SENSOR_DELAY_FASTEST);    
    }
     
    @Override
    protected void onStop()
    {
//        if (Config.LOGD) Log.d(TAG, "onStop");
        mSensorManager.unregisterListener(aLSListener);
        super.onStop();
    }
    
    /*public boolean onTouchEvent(MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN )
        {
            showDialog(0);            
        }
        return true;
    }*/
    
    @Override
    protected Dialog onCreateDialog(int none)
    {        
        return new AlertDialog.Builder(AmbientLightSensor.this)
//        .setIcon(R.drawable.alert_dialog_icon)
        .setTitle(R.string.SERVICEMENU_DIALOG_TITLE_TXT)
        .setPositiveButton(R.string.SERVICEMENU_OK_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked OK so do some stuff */
                finish();
            }
        })
        .setNegativeButton(R.string.SERVICEMENU_CANCEL_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked Cancel so do some stuff */
                finish();
            }
        })
        .create();
    }
    
    @Override
    public void finish() {
        super.finish();
    }
}
